package org.jeecg.modules.putstorage.service.impl;

import org.jeecg.modules.putstorage.entity.ZcglPutStorage;
import org.jeecg.modules.putstorage.entity.ZcglPutStorageAssets;
import org.jeecg.modules.putstorage.mapper.ZcglPutStorageAssetsMapper;
import org.jeecg.modules.putstorage.mapper.ZcglPutStorageMapper;
import org.jeecg.modules.putstorage.service.IZcglPutStorageService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 物资入库表
 * @Author: jeecg-boot
 * @Date:   2022-10-17
 * @Version: V1.0
 */
@Service
public class ZcglPutStorageServiceImpl extends ServiceImpl<ZcglPutStorageMapper, ZcglPutStorage> implements IZcglPutStorageService {

	@Autowired
	private ZcglPutStorageMapper zcglPutStorageMapper;
	@Autowired
	private ZcglPutStorageAssetsMapper zcglPutStorageAssetsMapper;

	@Override
	public int totayCount() {
		return zcglPutStorageMapper.totayCount();
	}

	@Override
	@Transactional
	public void saveMain(ZcglPutStorage zcglPutStorage, List<ZcglPutStorageAssets> zcglPutStorageAssetsList) {
		zcglPutStorageMapper.insert(zcglPutStorage);
		if(zcglPutStorageAssetsList!=null && zcglPutStorageAssetsList.size()>0) {
			for(ZcglPutStorageAssets entity:zcglPutStorageAssetsList) {
				//外键设置
				entity.setPutStorageAssetsId(zcglPutStorage.getId());
				zcglPutStorageAssetsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ZcglPutStorage zcglPutStorage,List<ZcglPutStorageAssets> zcglPutStorageAssetsList) {
		zcglPutStorageMapper.updateById(zcglPutStorage);

		//1.先删除子表数据
		zcglPutStorageAssetsMapper.deleteByMainId(zcglPutStorage.getId());

		//2.子表数据重新插入
		if(zcglPutStorageAssetsList!=null && zcglPutStorageAssetsList.size()>0) {
			for(ZcglPutStorageAssets entity:zcglPutStorageAssetsList) {
				//外键设置
				entity.setPutStorageAssetsId(zcglPutStorage.getId());
				zcglPutStorageAssetsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		zcglPutStorageAssetsMapper.deleteByMainId(id);
		zcglPutStorageMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			zcglPutStorageAssetsMapper.deleteByMainId(id.toString());
			zcglPutStorageMapper.deleteById(id);
		}
	}

	@Override
	public List<ZcglPutStorage> listByAsset(Map map) {
		return zcglPutStorageMapper.listByAsset(map);
	}

}
